﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Dynamic;
using System.Data.SqlClient;
using System.Configuration;
using Common;

    public class CommonDAL
    {
        public static string[] databases=Commons.GetConfig("DataBase").Split(',');
        public static string conn = "";

        public static bool ExecuteNoneQuery(string text, SqlParameter[] para)
        {
            int result = SQLHelper.ExecuteNonQuery(conn, CommandType.Text, text, para);
            return result == 0 ? false : true;
        }
        

        public static DataTable GetDataTable(string text)
        {
            conn = "Data Source=" + Commons.GetConfig("Server") + ";Initial Catalog=" + databases[Brower.Form1.index % databases.Length] + ";User ID=" + Commons.GetConfig("UserName") + ";Password=" + Commons.GetConfig("UserPwd");
            return SQLHelper.GetDataTable(conn, CommandType.Text, text, null);
        }


        public static List<POrderInfo> FindOrder()
        {
            DataTable dt = CommonDAL.GetDataTable("select * from porder where state=1 and Logistics='顺丰快递' and DateDiff(dd, CreateDate, GetDate()) <= 30 order by id desc");
            List<POrderInfo> list = new List<POrderInfo>();
            foreach (DataRow row in dt.Rows)
            {
                POrderInfo dy = new POrderInfo();
                dy.ID = DataConvert.ToInt(row["ID"]);
                dy.ExpressNum = DataConvert.ToString(row["ExpressNum"]);
                list.Add(dy);
            }
            return list;
        }

        public static bool UpdateTrail(int id,string trail)
        {
            string sql = string.Format("update [porder] set [Trail]=@Trail where [ID]=@ID");
            SqlParameter[] paras = new SqlParameter[]{
                new SqlParameter("@Trail", trail),
                new SqlParameter("@ID", id)
            };
            return CommonDAL.ExecuteNoneQuery(sql,paras);
        }


    }

